package com.dayu.share.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dayu.share.domain.ShareGoodsSpecs;
import com.dayu.share.domain.ShareGoodsSpecsValues;
import com.dayu.share.mapper.ShareGoodsSpecsMapper;
import com.dayu.share.mapper.ShareGoodsSpecsValuesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.share.mapper.ShareGoodsMapper;
import com.dayu.share.domain.ShareGoods;
import com.dayu.share.service.IShareGoodsService;

/**
 * 商品信息Service业务层处理
 * 
 * @author HK
 * @date 2021-04-08
 */
@Service
public class ShareGoodsServiceImpl implements IShareGoodsService 
{
    @Autowired
    private ShareGoodsMapper shareGoodsMapper;
    @Autowired
    private ShareGoodsSpecsMapper goodsSpecsMapper;
    @Autowired
    private ShareGoodsSpecsValuesMapper goodsSpecsValuesMapper;

    /**
     * 查询商品信息
     * 
     * @param goodsId 商品信息ID
     * @return 商品信息
     */
    @Override
    public ShareGoods selectShareGoodsById(Long goodsId)
    {
        return shareGoodsMapper.selectShareGoodsById(goodsId);
    }

    /**
     * 查询商品信息列表
     * 
     * @param shareGoods 商品信息
     * @return 商品信息
     */
    @Override
    public List<ShareGoods> selectShareGoodsList(ShareGoods shareGoods)
    {
        return shareGoodsMapper.selectShareGoodsList(shareGoods);
    }

    /**
     * 新增商品信息
     * 
     * @param shareGoods 商品信息
     * @return 结果
     */
    @Override
    public int insertShareGoods(ShareGoods shareGoods)
    {
        return shareGoodsMapper.insertShareGoods(shareGoods);
    }

    /**
     * 修改商品信息
     * 
     * @param shareGoods 商品信息
     * @return 结果
     */
    @Override
    public int updateShareGoods(ShareGoods shareGoods)
    {
        return shareGoodsMapper.updateShareGoods(shareGoods);
    }

    /**
     * 批量删除商品信息
     * 
     * @param goodsIds 需要删除的商品信息ID
     * @return 结果
     */
    @Override
    public int deleteShareGoodsByIds(Long[] goodsIds)
    {
        return shareGoodsMapper.deleteShareGoodsByIds(goodsIds);
    }

    /**
     * 删除商品信息信息
     * 
     * @param goodsId 商品信息ID
     * @return 结果
     */
    @Override
    public int deleteShareGoodsById(Long goodsId)
    {
        return shareGoodsMapper.deleteShareGoodsById(goodsId);
    }

    /**
     * 规格转换为前端格式
     * @param shareGoods
     * @return
     */
    @Override
    public  List<Map<String, Object>> specsMapTranModelMap(ShareGoods shareGoods) {
        JSONArray specsArr = JSON.parseArray(shareGoods.getSpecsMap());
        Map<String, String> map = new HashMap<>();

        //转换为父子结构
        for(Object arr: specsArr) {
            String[] specs = arr.toString().replace("[","").replace("]","").split(",");
            //如果是安卓机，不转换颜色
            if(!(shareGoods.getBrandId() == 1 || shareGoods.getBrandId() == 2)) {
                if("2".equals(specs[0])) {
                    continue;
                }
            }
            map.merge(specs[0], specs[1], (a, b) -> a + "," + b);
        }
        //获取父级列表
        List<ShareGoodsSpecs> specsList = goodsSpecsMapper.selectSpecsListByList(map.keySet());
        //查询参数
        ShareGoodsSpecsValues selectValue = new ShareGoodsSpecsValues();
        Map<String, Object> params = new HashMap<>();

        //返回结果
        List<Map<String, Object>> respList = new ArrayList<>();
        for (ShareGoodsSpecs specs: specsList) {
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("specsName", specs.getSpecsName());  //规格名称
            resMap.put("specsId", specs.getSpecsId());  //编号
            resMap.put("sort", specs.getSpecsSort());   //排序
            resMap.put("selected", 0);   //是否选中

            //获取子节点
            List<Map<String, Object>> child = new ArrayList<>();
            String[] valueIds = map.get(specs.getSpecsId().toString()).split(",");
            params.put("ids", valueIds);
            selectValue.setParams(params);
            List<ShareGoodsSpecsValues> valuesList = goodsSpecsValuesMapper.selectShareGoodsSpecsValuesList(selectValue);

            for(ShareGoodsSpecsValues values: valuesList) {
                Map<String, Object> childMap = new HashMap<>();
                childMap.put("valueId", values.getValuesId());
                childMap.put("parentId", values.getSpecsId());
                childMap.put("sort", values.getValuesSort());
                childMap.put("valueName", values.getValuesName());
                child.add(childMap);
            }
            resMap.put("children", child);
            respList.add(resMap);
        }
        return respList;
    }
}
